function [XD] = data_stats(varargin)
%
% this routine computes the data statistics for a selected list of either
% fields, or stucture/substructure elements
% broken down by unique combinations pnd/div/E_C/G1/G2
% and withing file.
% Handles 2-dimensional data set
% e.g., by varargin{1} and varargin{2}.
% 1 is the experimental group identifier (E_C); 2 is the treatment
% (in G1)
% arguments

global CONTROL

sel = getmainselection;
fn = unique({CONTROL(sel).filename});
pnd = unique([CONTROL(sel).pnd]);
div = unique([CONTROL(sel).div]);
E_C = unique({CONTROL(sel).E_C});
if(ischar(CONTROL(sel(1)).G1));
    G1 = unique({CONTROL(sel).G1});
else
    G1 = unique([CONTROL(sel).G1]);
end;
if(ischar(CONTROL(sel(1)).G2))
    G2 = unique({CONTROL(sel).G2});
else
    G2 = unique([CONTROL(sel).G2]);
end;

ngroup = length(E_C);
ntreat = length(G1);
XD = NaN*zeros(ntreat, ngroup, nargin);

for idata = 1:nargin % for each variable to be examined
    for icell = 1:length(fn) % do for each cell
        fsel = strmatch(fn(icell), {CONTROL(sel).filename}, 'exact');
        for iincell = 1:length(fsel)
            ic = fsel(iincell);
            X = getstructs(iincell, varargin{idata});
            fprintf(1, 'file: %s  var: %s\n', CONTROL(iincell).filename, varargin{idata});
            ng = strmatch(CONTROL(ic).E_C, E_C, 'exact');
            nt = strmatch(CONTROL(ic).G1,G1,  'exact');
            if(~isnan(ng) && ~isnan(nt))
                XD(nt, ng, icell) = cell2mat(X);
            else
                XD(nt, ng, icell) = NaN;
            end;
        end;
    end;

end;

XD
